﻿using System;
using System.Collections.Generic;
using System.Text;

namespace ebay.Utility
{
    public class PubClass
    {
        public static DateTime MinDateTime = new DateTime(1900, 1, 1);

        public static T GetEnum<T>(string val) {
            if (!typeof(T).IsEnum) {
                throw new Exception("类型T不是有效的枚举类型");
            }
            return (T)Enum.Parse(typeof(T), val);
        }

        public static string GetString(string s) {
            return string.IsNullOrEmpty(s) ? "" : s;
        }

        public static DateTime GetDateTime(DateTime dt) {
            return dt < MinDateTime ? MinDateTime : dt;
        }

        public static Nullable<double> ToDouble(object objValue) {
            try {
                return Convert.ToDouble(objValue);
            } catch (System.Exception) {
                return null;
            }
        }

        public static string GenerateStringID() {
            long i = 1;
            foreach (byte b in Guid.NewGuid().ToByteArray()) {
                i *= ((int)b + 1);
            }
            return string.Format("{0:x}", i - DateTime.Now.Ticks);
        }
    }
}
